package com.mxpio.erp.plm.service.impl;

import com.mxpio.erp.common.plm.entity.ProjectInfo;
import com.mxpio.erp.common.plm.entity.VEProjectOverdue;
import com.mxpio.erp.common.plm.entity.VEProjectType;
import com.mxpio.erp.common.plm.service.VEProjectOverdueService;
import com.mxpio.erp.common.quality.entity.VEInspectionBillAssemblyProblem;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.SimpleCriterion;
import com.mxpioframework.security.entity.Dept;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Service
public class VEProjectOverdueServiceImpl implements VEProjectOverdueService {

//    @Override
//    @Transactional
//    public List<VEProjectOverdue> list(Criteria c) {
//        return JpaUtil.linq(VEProjectOverdue.class).where(c).list();
//    }
//
//    @Override
//    @Transactional
//    public Page<VEProjectOverdue> listPage(Pageable pageAble, Criteria c) {
//        return JpaUtil.linq(VEProjectOverdue.class).where(c).paging(pageAble);
//    }

    @Override
    @Transactional
    public List<VEProjectOverdue> getlist(Criteria criteria) {
        List<Object> criterions = criteria.getCriterions();
        List<String> codes = new ArrayList<>();
        if (!criterions.isEmpty()){
            Iterator<Object> iterator = criterions.iterator();
            while(iterator.hasNext()){
                Object next = iterator.next();
                if (next instanceof SimpleCriterion){
                    SimpleCriterion simpleCriterion = (SimpleCriterion)next;
                    if (StringUtils.equals(simpleCriterion.getFieldName(),"infoDeptId")){
                        String value = (String) simpleCriterion.getValue();
                        //查询部门以及子部门
                        Dept dept = JpaUtil.linq(Dept.class).equal("deptCode", value).findOne();
                        List<String> deptCodes    =  getchildren(dept);
                        codes.add(value);
                        codes.addAll(deptCodes);
                    }
                }
            }
        }
        Criteria c = Criteria.create();
        c.addCriterion("infoDeptId", Operator.IN,codes);////对应的部门子级

        Linq linq =JpaUtil.linq(VEProjectOverdue.class);
        return linq.aliasToBean(VEProjectOverdue.class).select("overdueDayType","infoDeptId",
                        linq.criteriaBuilder().sum(linq.root().get("num")).alias("num"))
                .where(c)
                .groupBy("overdueDayType","infoDeptId")
                .list();
    }


    /**
     * 获取部门下所有子部门的编码
     * @param dept
     * @return
     */
    private List<String> getchildren(Dept dept) {
        List<String> deptCodes = new ArrayList<>();
        List<Dept> deptList = JpaUtil.linq(Dept.class).equal("faDeptId", dept.getId()).list();
        if (deptList != null && deptList.size() > 0) {
            for (Dept dept1 : deptList) {
                deptCodes.add(dept1.getDeptCode());
                deptCodes.addAll(getchildren(dept1));
            }
        }
        return deptCodes;
    }
}
